package com.specialtreasure.freightdepot.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.app.core.activity.BaseNewActivity;
import com.app.core.config.Constants;
import com.app.core.config.HttpApi;
import com.app.core.util.UserUtils;
import com.specialtreasure.freightdepot.R;
import com.specialtreasure.freightdepot.model.BaseModel;
import com.specialtreasure.freightdepot.popupwindow.SelectUnitPW;
import com.yanzhenjie.httpmodule.http.CallServer;
import com.yanzhenjie.httpmodule.http.Result;
import com.yanzhenjie.httpmodule.http.StringRequest;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.CacheMode;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;

/**
 * Created by Administrator on 2017/8/7 0007.
 * 添加联系人
 */

public class LinkManActivity extends BaseNewActivity implements View.OnClickListener {

    public static final String TAG = "LinkManActivity";
    private ImageView iv_back;
    private TextView tv_title;
    private EditText et_add_phone;
    private RelativeLayout rl_chufadi;
    private TextView tv_sure;
    private TextView tv_group;
    private RelativeLayout rl_root;
    private Integer group;//(0.自有 1.外协)
    String carPhone;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_add_linkman);
        initView();
        setData();
    }


    private void setData() {
        tv_title.setText("添加手机联系人");
        show(tv_sure);
        show(iv_back);
    }


    @Override
    public String getActiviyName() {
        return TAG;
    }

    private void initView() {
        iv_back = (ImageView) findViewById(R.id.iv_back);
        tv_title = (TextView) findViewById(R.id.tv_title);
        et_add_phone = (EditText) findViewById(R.id.et_add_phone);
        rl_chufadi = (RelativeLayout) findViewById(R.id.rl_chufadi);
        rl_root = (RelativeLayout) findViewById(R.id.rl_root);

        iv_back.setOnClickListener(this);
        tv_sure = (TextView) findViewById(R.id.tv_sure);
        tv_sure.setOnClickListener(this);
        tv_group = (TextView) findViewById(R.id.tv_group);
        rl_chufadi = (RelativeLayout) findViewById(R.id.rl_chufadi);
        rl_chufadi.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back://返回
                finish();
                break;
            case R.id.tv_sure://确定
                showDialog(Constants.LOADING);
                submit();
                break;
            case R.id.rl_chufadi://添加到分组
                SelectUnitPW supw8 = new SelectUnitPW(this, "fenzu.xml", "分组");
                supw8.showAtBottom(rl_root);
                supw8.setOnResultListener(new SelectUnitPW.OnResultListener() {
                    @Override
                    public void onResult(String str) {
                        if (!TextUtils.isEmpty(str)) {
                            tv_group.setText(str);
                            tv_group.setTextColor(mContext.getResources().getColor(R.color.common_474747));
                            if (str.equals("不限")) {
                                group = null;
                            } else if (str.equals("自有")){
                                group = 0;
                            }else {
                                group =1;
                            }

                        }
                    }
                });
                break;
        }
    }

    private void submit() {
        // validate
        String phone = et_add_phone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入司机手机号", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something
        getData();


    }

    private void getData() {
        String url = Constants.HTTP_URL + HttpApi.ADDDRIVERBYPHONE;
        StringRequest request = new StringRequest(url, RequestMethod.POST);
        request.add("token", UserUtils.getToken());
        request.add("carPhone", et_add_phone.getText().toString().trim());
        request.add("group", group);


        request.setCacheMode(CacheMode.ONLY_REQUEST_NETWORK);
        CallServer.getInstance().request(Constants.ZERO, request, new SimpleResponseListener<Result<String>>() {
            @Override
            public void onSucceed(int what, Response<Result<String>> response) {
                super.onSucceed(what, response);
                closeDialog();

                String result = response.get().getResult();
                Logger.e("请求成功：" + result);
                BaseModel baseModel = JSON.parseObject(result, BaseModel.class);
                if (baseModel != null) {
                    Integer status = baseModel.getStatus();
                    String msg = baseModel.getMsg();
                    if (status != null && status == 0) {
                        Toast.makeText(mContext, "添加联系人成功", Toast.LENGTH_SHORT).show();
                        et_add_phone.getText().clear();
                        tv_group.setText("");
                    } else {
                        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                    }
                }


            }
        });

    }
}
